﻿Public Class TipoNotaCreditoDAO
    Public Shared Function insertarTipoNotaCredito(ByVal TipoNotaCreditoNombre As String, ByVal TipoNotaCreditoDescripcion As String) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_TipoNotaCredito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoNombre", TipoNotaCreditoNombre).DbType = DbType.String
                    .AddWithValue("@TipoNotaCreditoDescripcion", TipoNotaCreditoDescripcion).DbType = DbType.String                    
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerTiposNotasCredito(ByVal TipoNotaCreditoId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_TipoNotaCredito_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@TipoNotaCreditoId", TipoNotaCreditoId).DbType = DbType.Int32
            da.Fill(ds, "TiposNotasCredito")

            Return ds.Tables("TiposNotasCredito")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerTipoNotaCreditoId(ByVal TipoNotaCreditoNombre As String) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand
            cmd.Connection = cn
            cmd.CommandText = "Select TipoNotaCreditoId From TipoNotaCredito Where TipoNotaCreditoNombre Like '" & TipoNotaCreditoNombre & "'"
            Dim TipoNotaCreditoId As Integer = cmd.ExecuteScalar

            Return TipoNotaCreditoId
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarTipoNotaCredito(ByVal TipoNotaCreditoId As Integer, ByVal TipoNotaCreditoNombre As String, ByVal TipoNotaCreditoDescripcion As String) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_TipoNotaCredito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoId", TipoNotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoNombre", TipoNotaCreditoNombre).DbType = DbType.String
                    .AddWithValue("@TipoNotaCreditoDescripcion", TipoNotaCreditoDescripcion).DbType = DbType.String                    
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarTipoNotaCredito(ByVal TipoNotaCreditoId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_TipoNotaCredito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoId", TipoNotaCreditoId).DbType = DbType.Int32
                    .AddWithValue("@TipoNotaCreditoNombre", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@TipoNotaCreditoDescripcion", Constante.VACIO).DbType = DbType.String
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function comboFiltroTipoNotaCredito() As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_tipoNotaCredito_comboFiltro", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.Fill(ds, "Tipos")

            Return ds.Tables("Tipos")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

End Class

